<?php

/**
 *  库存表
 */

namespace app\api\model;

use think\facade\Lang;

class Inventory extends BaseModel
{

    // protected $append = ['futures_text'];

    protected $attributes = [
        'is_futures' => [
            0 => 'inventory.futures',
            1 => 'inventory.spots',
            2 => 'inventory.bigFutures',
        ]
    ];

    /**
     *  期货字段
     */
    public function getFuturesTextAttr($vaule, $data)
    {
        $attributes = $this->attributes;
        if (isset($attributes['is_futures'][$data['is_futures']])) {
            return Lang::get($attributes['is_futures'][$data['is_futures']]);
        } else {
            return Lang::get('inventory.error');
        }
    }

    /**
     *  价格
     */
    public function price()
    {
        return $this->hasOne('ProductPrice', 'inventory_id', 'id');
    }


    /* 仓库 */
    public function warehouse()
    {
        return $this->hasOne('Warehouse', 'id', 'wid');
    }

    /* 商品 */
    public function product()
    {
        return $this->hasOne('Product', 'id', 'productid');
    }



    public function item()
    {
        return $this->hasOne('Product', 'id', 'productid');
    }


    public function brands()
    {
        return $this->hasOne('ProductBrand', 'id', 'brand');
    }

    /**
     *  折扣阶梯价
     */
    public function discount()
    {
        return $this->hasMany('ProductDiscount', 'inventoryid', 'id');
    }

    // 在Inventory模型中添加新的关联
    public function priceDiscounts()
    {
        return $this->hasMany('ProductDiscount', 'price_id', 'price_id');
    }


    /**
     *  过去订单状态
     */
    public function getFieldEnum($field)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'is_futures' => $this->attributes['is_futures']
            ];
        } else {
            return [$field => $this->$field];
        }
    }
}
